/**
 * Created by baiguiren on 1/18/17.
 */

let async = require('async');
let fs = require('fs');
let path = require('path');

async.auto({
  get_data: (callback) => {
    console.log('in get data...');
    let temp_data;
    fs.readFile(__dirname + '/data.txt', 'utf-8', function read(err, data) {
      temp_data = data;
      callback(null, JSON.stringify(temp_data));
    });
  },
  make_dir: (callback) => {
    console.log('in make dir...');
    if (!path.exists(__dirname + '/temp')) {
      fs.mkdir(__dirname + '/temp', function (err, folder) {
        if (err) {
          console.log(err);
        }
      });
    }
    callback(null);
  },
  write_file: ['get_data', 'make_dir', (results, callback) => {
    console.log('in write file...');
    results += '\nnew end.';
    fs.writeFile(__dirname + '/temp/new_data.txt', results, 'utf-8', function write(err, data) {
      if (err) {
        console.log(err);
      }
    });
    callback(null);
  }]
}, (err, results)=>{
  console.log('err = ', err);
  console.log('results = ', results);
});
